﻿//----------------------------------------------
// 作者：曾同春
// 日期：2015-12-01
// 功能：出口页面控件权限分配(注意：调用函数必须在mini.parse()后)
// ----------------------------------------------------------

//初始化页面form能拿到的控件
var initDom = [];
var fromDom = new mini.Form("#form1");
var domArray = fromDom.getFields();
for (var i = 0; i < domArray.length; i++) {
    var id = domArray[i].id;
    if (id.indexOf("mini") < 0) {
        initDom.push(id);
    }
}
//初始化页面form不能拿到的控件
var notFind = ["btnSave", "btnLogic", "btnQpInput", "btnQpDownload", "btnLock", "btnCopy", "btnPaste", "btnReq",
    "btnCreate", "btnAudit", "btnPrint", "btnAddGoods", "btnDeleteGoods", "btnAddDoc", "btnDeleteDoc",
 "btnTong", "btnTong1", "btnGoodsExport", "btnContinueAdd", "btnContainerAdd", "btnContainerDel", "btnPlatInfo", "btnJiuCuo",
"datagrid6", "datagrid1", "datagrid5", "datagrid2", "check_info"];
for (var i = 0; i < notFind.length; i++) {
    initDom.push(notFind[i]);
}

// type:权限类型 initEnable:初始化所有配置控件是否可操作(默认是可操作的true) enableIds:要操作的控件id集合  enable:是否可操作
var role = [
{
    //报关员
    type: 1, initEnable: true, enableIds: ["check_info", "txtjkhd_by", "txtsfdj_no", "datagrid5"
    ], enable: false
},
       {
           //外勤员
           type: 2, initEnable: false, enableIds: ["datagrid5", "btnSave", "check_info",
               "ckcheck_yn", "chkcheck01_yn", "jkhd_cost", "txtcost_note"
           ], enable: true
       },
       //{
       //    //查验员
       //    type: 3, initEnable: false, enableIds: ["datagrid5", "btnSave", "check_info",
       //        "ckcheck_yn", "chkcheck01_yn"
       //    ], enable: true
       //},
       {
           //报检员
           type: 4, initEnable: false, enableIds: ["datagrid5", "btnSave", "check_info",
                "txtsfdj_no", "ckcheck_yn", "chkcheck01_yn", "txtcost_note"
           ], enable: true
       }
];
function SetDomEnabled(ids) {
    var checkRole = mini.get("hidRoleCheck").getValue();//判断多角色兼职报关员的只能操作本组数据(0不可操作，1可操作)
    var idsArray = ids.split(",");
    idsArray = Enumerable.From(idsArray).OrderBy();//按顺序排序
    var init = 0;//初始化配置只能操作一次;
    //如果角色不在本组过滤掉报关员的权限
    if (checkRole == "0" && ids.indexOf("1") >= 0) {
        idsArray = Enumerable.From(idsArray).Where(function (i) {
            return i > 1;
        }).ToArray();
        ids = idsArray.join(",");

    }

    Enumerable.From(idsArray).ForEach(function (id) {
        var obj = Enumerable.From(role).Where(function (x) {
            return x.type == id;
        }).ToArray()[0];
        if (ids.indexOf("1") < 0) {//没有报关员权限时初始化页面控件为不可操作
            //是否需要初始化所有配置控件为不可操作
            if (!obj.initEnable && init == 0) {
                //陆运报检员要求有编辑商品的权限
                if (!(obj.type == 4 && mini.get("hidtransport_way").getValue() == "02")) {
                    mini.get("datagrid2").o10olo.rowdblclick = null;//删除商品表体的双击事件
                }
                Enumerable.From(initDom).ForEach(function (x) {
                    var obj = mini.get(x);
                    if (obj != null) {
                        mini.get(x).setEnabled(false);
                    }
                });
                init = 1;
            }
        }

        //是否可操作
        if (obj.enable) {
            //循环给控件设置属性enable=true
            Enumerable.From(obj.enableIds).ForEach(function (x) {
                var obj = mini.get(x);
                if (obj != null) {
                    mini.get(x).setEnabled(true);
                }
                if (x == "check_info") {//查询框
                    ChaYanInfo();
                }
            });
        } else {
            //循环给控件设置属性enable=false
            Enumerable.From(obj.enableIds).ForEach(function (x) {
                var obj = mini.get(x);
                if (obj != null) {
                    mini.get(x).setEnabled(false);
                }
            });
        }
    });

}
//查验信息
function ChaYanInfo() {
    var haiguan = new mini.Form("#check_haiguang");
    var haiguanArray = haiguan.getFields();
    for (var i = 0; i < haiguanArray.length; i++) {
        mini.get(haiguanArray[i].id).setEnabled(true);
    }
    var shangjian = new mini.Form("#check_shangjian");
    var shangjianArray = shangjian.getFields();
    for (var i = 0; i < shangjianArray.length; i++) {
        mini.get(shangjianArray[i].id).setEnabled(true);
    }

}